Progress made

Katya - Loaded Google maps using ggmap and plotted the latitudes and longitudes of earthquakes in our data set directly onto the Google maps. Because the extent of the earthquakes is so large, I created two different base maps: one centered on Point Heiden, Alaska and the other on Los Angeles.

Katja - Worked to add a time_til_quake variable to the dataset. This would give the number of days before/after the nearest major >7 earthquake (both timewise and spacially) that each foreshock/aftershock occurred. So far, I have written half of a function that would create this variable for us without doing it by hand. The function FindMaxMagRow takes in a dataset and returns the row of the earthquake with the largest magnitude within that set. The function CreateTimeToDays will create the time_til_quake variable so it can be attached to the dataset. It compares the time of each quake to the time of the largest quake to get the difference.

cleanQuakes = read.csv("cleanQuakes.csv")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
cleanQuakes1 <- cleanQuakes %>%
  select(-c(X.1))
library(stringr)
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.4.2
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
#Creating a year, month, and day columns and adding to cleanQuakes1
Year_ <- year(cleanQuakes1$time)
Month_ <- month(cleanQuakes1$time)
Day_ <- day(cleanQuakes1$time)
cleanQuakes1 <- cbind(cleanQuakes1, Year_)
cleanQuakes1 <- cbind(cleanQuakes1, Month_)
cleanQuakes1 <- cbind(cleanQuakes1, Day_)

#lapply(cleanQuakes1$time, as.character)

cleanQuakes1$time <- substring(cleanQuakes1$time, 1, 19)
time_of_Quake <- str_sub(cleanQuakes1$time, -8,-1)
cleanQuakes1 <- cbind(cleanQuakes1, time_of_Quake)

cleanQuakesFin <- cleanQuakes1 %>%
  select(-c(time))
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.4.2
## Loading required package: ggplot2
centerLA <- get_map(location = "los angeles",
    color = "color",
    source = "google",
    maptype = "satellite",
    zoom = 4)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=los+angeles&zoom=4&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=los%20angeles&sensor=false
centerPH <- get_map(location = "port heiden",
    color = "color",
    source = "google",
    maptype = "satellite",
    zoom = 4)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=port+heiden&zoom=4&size=640x640&scale=2&maptype=satellite&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=port%20heiden&sensor=false
ggmap(centerLA,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

ggmap(centerPH,
    extent = "device",
    ylab = "Latitude",
    xlab = "Longitude")
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead

library(dplyr)
quakesAbove7 <- filter(cleanQuakesFin, mag >= 7)
quakesSince1950 <- filter(cleanQuakesFin, Year_ >= 1950)
quakesAbove7Since1950 <- filter(cleanQuakesFin, Year_ >=1950, mag >= 7)
library(mapproj)
## Warning: package 'mapproj' was built under R version 3.4.2
## Loading required package: maps
## Warning: package 'maps' was built under R version 3.4.2
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
# color by magnitude, all earthquakes
map1 <- ggmap(centerLA) + geom_point(data=cleanQuakes, aes(x = longitude, y =latitude, color=mag), size = 0.5) +
  scale_color_gradient(low="lightsalmon", high="darkred")
map2 <- ggmap(centerPH) + geom_point(data=cleanQuakes, aes(x=longitude, y=latitude, color=mag), size = 0.5) +
  scale_color_gradient(low="lightsalmon", high="darkred")

# color by magnitude, earthquakes above 7
map3 <- ggmap(centerLA) + geom_point(data=quakesAbove7, aes(x = longitude, y =latitude, color=mag), size = 1) +
  scale_color_gradient(low="lightsalmon", high="darkred")
map4 <- ggmap(centerPH) + geom_point(data=quakesAbove7, aes(x=longitude, y=latitude, color=mag), size = 1) +
  scale_color_gradient(low="lightsalmon", high="darkred")

grid.arrange(map1, map2, map3, map4, ncol=2)
## Warning: Removed 2670 rows containing missing values (geom_point).
## Warning: Removed 1261 rows containing missing values (geom_point).
## Warning: Removed 58 rows containing missing values (geom_point).
## Warning: Removed 41 rows containing missing values (geom_point).

#also color by year
# color by year, all earthquakes since 1950
map5 <- ggmap(centerLA) + geom_point(data=quakesSince1950, aes(x = longitude, y =latitude, color=Year_), size = 0.5) +
  scale_color_gradient(low="chartreuse", high="chartreuse4")
map6 <- ggmap(centerPH) + geom_point(data=quakesSince1950, aes(x=longitude, y=latitude, color=Year_), size = 0.5) +
  scale_color_gradient(low="chartreuse", high="chartreuse4")

# color by year, earthquakes above 7 since 1950
map7 <- ggmap(centerLA) + geom_point(data=quakesAbove7Since1950, aes(x = longitude, y =latitude, color=Year_), size = 1) +
  scale_color_gradient(low="chartreuse", high="chartreuse4")
map8 <- ggmap(centerPH) + geom_point(data=quakesAbove7Since1950, aes(x=longitude, y=latitude, color=Year_), size = 1) +
  scale_color_gradient(low="chartreuse", high="chartreuse4")

grid.arrange(map5, map6, map7, map8, ncol=2)
## Warning: Removed 2625 rows containing missing values (geom_point).
## Warning: Removed 1212 rows containing missing values (geom_point).
## Warning: Removed 42 rows containing missing values (geom_point).
## Warning: Removed 30 rows containing missing values (geom_point).

#Create time until big quake or after quake variable - the function Katja's been writing

HIQuake1975 <- filter(cleanQuakes1,  X < 25)
HIQUake1975 <- filter(HIQuake1975, latitude < 50)


FindMaxMagRow <- function(datasub){
  #Finds the row within the input data that has the maximum magnitude
  currentMax = 0
  maxrow = 0
  for(i in 1:nrow(datasub)){
    magnitude = datasub$mag[[i]]
    if(magnitude > currentMax){
      currentMax = magnitude
      maxrow =  i
    }
  }
  return(maxrow)
}



CreateTimeToDays <- function(Qdata){
  #Takes a dataset as an input and adds on a time_til_Quake variable that contains the number of days prior to or after the biggest quake in that dataset; For example if the largest quake occurred on January 5, 1988 at 6 pm, then a quake that occurred in the same region on January 4, 1988 at 5pm would be assigned time_til_Quake =(1.0417). It will be negative if it is an aftershock.
  MaxMagRow = FindMaxMagRow(Qdata)
  QuakeYear = Qdata$Year_[[MaxMagRow]]
  QuakeMonth = Qdata$Month_[[MaxMagRow]]
  QuakeDay = Qdata$Day_[[MaxMagRow]]
  QuakeTime = Qdata$time_of_Quake[[MaxMagRow]]
  QuakeHour = hour(QuakeTime)
  time_til_Quake = rep(0, nrow(Qdata))
  HoursToQuake = 0
  for ( i in 1:nrow(Qdata)){
     if(Qdata$time_of_Quake[[i]] == QuakeTime){
            time_til_Quake[i] = 0
     }
    else{
      if(Qdata$Year_[[i]] == QuakeYear){
        if(Qdata$Month_[[i]] == QuakeMonth){
          if(Qdata$Day_[[i]] == QuakeDay){
            #1. newhour: Get hours from Qdata$Time_of_Quake[[i]]
            time_til_Quake[i] = (QuakeHour-newhour)/24
          }
          else{
            DayDiff = QuakeDay-Qdata$Day_[[i]]
            if(DayDiff==1){
              newhour = 24-(hour(time_of_Quake))
              time_til_Quake[i] = (newhour + QuakeHour)/24
            }
            else{ #if not one but still positive?
              newhour = 24-(hour(time_of_Quake))
              NewDayDiff = DayDiff-1
              newhour = newhour + (NewDayDiff*24)
              time_til_Quake[i] = (newhour + QuakeHour)/24
            }
          }
      }
    }
    }
    
  }
  
}
summary(cleanQuakesFin)
##        X           latitude       longitude           depth       
##  Min.   :   1   Min.   :11.47   Min.   :-179.99   Min.   :  0.00  
##  1st Qu.: 421   1st Qu.:51.09   1st Qu.:-174.07   1st Qu.: 18.10  
##  Median :1082   Median :51.61   Median :-153.70   Median : 33.00  
##  Mean   :1189   Mean   :47.51   Mean   : -92.87   Mean   : 35.68  
##  3rd Qu.:1922   3rd Qu.:53.04   3rd Qu.:-101.17   3rd Qu.: 35.00  
##  Max.   :2762   Max.   :67.55   Max.   : 180.00   Max.   :274.80  
##                                                                   
##       mag           magType              id      
##  Min.   :4.500   mb     :2088   ak11695807:   2  
##  1st Qu.:4.600   mw     : 642   ak11778117:   2  
##  Median :4.900   mwc    : 233   ak12107786:   2  
##  Mean   :5.129   ml     : 101   ak12465857:   2  
##  3rd Qu.:5.500   mww    :  90   ak12498869:   2  
##  Max.   :9.200   mwr    :  76   ak12542862:   2  
##                  (Other): 131   (Other)   :3349  
##                                          place               type     
##  Andreanof Islands, Aleutian Islands, Alaska: 743   earthquake :3360  
##  Fox Islands, Aleutian Islands, Alaska      : 335   other event:   1  
##  Rat Islands, Aleutian Islands, Alaska      : 258                     
##  Kodiak Island region, Alaska               : 101                     
##  Near Islands, Aleutian Islands, Alaska     :  92                     
##  Central Alaska                             :  86                     
##  (Other)                                    :1746                     
##  locationSource   magSource        Year_          Month_      
##  us     :2251   us     :2291   Min.   :1911   Min.   : 1.000  
##  iscgem : 509   iscgem : 505   1st Qu.:1981   1st Qu.: 4.000  
##  aeic   : 218   hrv    : 256   Median :1996   Median : 6.000  
##  ak     : 162   gcmt   :  84   Mean   :1992   Mean   : 6.548  
##  ci     :  63   pgc    :  80   3rd Qu.:2010   3rd Qu.:10.000  
##  ags    :  49   ci     :  63   Max.   :2016   Max.   :12.000  
##  (Other): 109   (Other):  82                                  
##       Day_        time_of_Quake 
##  Min.   : 1.00   10:55:35:   3  
##  1st Qu.: 8.00   21:33:10:   3  
##  Median :15.00   00:05:13:   2  
##  Mean   :15.31   00:09:15:   2  
##  3rd Qu.:23.00   00:42:22:   2  
##  Max.   :31.00   00:54:07:   2  
##                  (Other) :3347
sum(cleanQuakesFin$mag>=7)
## [1] 88